<?php
/*
 To add a field
  1. Add to DB
  2. Add to interface Showform
  3. Add to applyfromrequest  
  4. Enter a default in createblank
*/
class jobs extends head {
 function jobs() {
  $this->table = "jobs";
  $this->getmetadata();
  $this->maintainhist = true;
  $this->header[2] = "";
  $_SESSION[$this->table] = $this;
 }
 function createblank() {
  parent::createblank();
  $this->header[5] = "N";
  $this->header[6] = "0";
  $this->header[7] = "00/00/0000";
  $this->header[8] = "0";
  $this->header[9] = "FFFFFF";

 } 
 
 function showform() {
  echo '<head>';
  echo "<script src='datechooser.js' type='text/javascript'></script>";
  echo SCRIPTS;
  echo '</head>';
	echo "<body onload='document.form.f0.focus()'>";
  echo "<h1>Jobs Setup</h1>"; 
  echo "<form name='form' method=GET>";
  echo "<input type=hidden name=res value=''>";
  echo "<table>";
  echo $this->htmlrow('0','Code');
  echo $this->htmlrow('1','Name');
  echo $this->htmlrow('5','Finished (Y/N)');
  echo $this->htmlrow('6','Standard Hours');
  echo $this->htmlrow('8','Materials Cost $');
  echo $this->htmlrow('9','Hex Colour');
  

  echo "<tr><td>Delivered Date</td><td>";
  $dt = isotoout($this->header[7]);
  echo "<input id=dot name=f7 size=10 maxlength=10 type=text value='$dt'>\n"; 
  if ($this->disabled != "disabled") {
   $dc = "showChooser(this, 'dot', 'chooserSpan', 1950, 2010, 'd-m-Y', false);";
   echo "<img src='calendar.gif' onclick=".'"'.$dc.'"'.">\n";
   echo "<div id='chooserSpan' class='dateChooser' style='display: none; visibility: hidden; width: 160px;'>\n";
   echo "</div>\n";
  }
  echo "</td>";
  echo "<td>".$this->valids[7]."</td>";  
  echo "</tr>";



  
  echo "</table>";
  echo '<input class=bt accesskey=A type=submit name=sub value=Apply>';
  echo '<input class=bt accesskey=S type=submit name=sub value=Save>';
  echo '<button class=bt onclick=rest()><span>Restart</span></button>'; 
  echo "</form>"; 
 }
 function applyfromrequest() {
  $this->valid = true;
  // Goes through request. Applies if legal, otherwise error.
    $this->valids = array_fill(0,$this->atts,'');

  if (isset($_REQUEST["f0"]) && $this->header[2] == "0") {
   // Numeric whole only
   $f = $_REQUEST["f0"];
   if(preg_match('/^[0-9A-Z]{1,10}$/',$f)) {
    $exists = freaditem($this->table,$f) ;
    if ($exists == "") {
     $this->header[0] = $f;
    } else {
     $this->valids[0] = "That ID is already in use for ".$exists[1];
    }
   } else {
    $this->valids[0] = "Please enter a one or two digit number";
   }
  }

  if (isset($_REQUEST["f1"])) {
   // name. Allowable characters A-Za-z-space apostrophe
   $f = $_REQUEST["f1"];
   if(preg_match('/^[0-9a-z A-Z\'\.-]{2,45}$/',$f)) {
    $this->header[1] = $f; 
   } else {
    $this->valids[1] = "Please enter a valid name";
   }
  }  
  if (isset($_REQUEST["f5"])) {
   // Discontinued
   $f = $_REQUEST["f5"];
   if ($f == "Y" || $f == "N") {
    $this->header[5] = $f; 
   } else {
    $this->valids[5] = "Please enter either Y or N";
   }
  }  

  if (isset($_REQUEST["f6"])) {
   // Standard Hours
   $f = $_REQUEST["f6"];
   if(preg_match('/^[0-9]{1,10}$/',$f)) {
    $this->header[6] = $f; 
   } else {
    $this->valids[6] = "Please enter a number";
   }
  }  
 
 if( $f=@$_REQUEST["f8"]) {
   // Integer dollars
   $a  = $f;
   if(preg_match('/^[0-9]{1,10}$/',$f)) {
     $this->header[8] = $a;
   } else {
    $this->valids[8] = "Enter an amount in dollars";
   }
  } 	

 if (isset($_REQUEST["f9"])) {
   // Colour
   $f = $_REQUEST["f9"];
   if(preg_match('/^([A-F]|[0-9]){6}$/',$f)) {
    $this->header[9] = $f; 
   } else {
    $this->valids[9] = "Please enter an RGB color in Hex, e.g. FF00FF";
   }
  } 
  
  
 
  $this->valid = every($this->valids,"");
  if ($this->valid) {
   if ($this->header[0] == "") $this->valids[0] = "Incomplete";
   if ($this->header[1] == "") $this->valids[1] = "Incomplete";
   $this->valid = every($this->valids,"");   
  }
   
 }
}
?>